package com.njh.check.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.njh.check.vo.CheckingResultImgVo;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @program: gz01-springmvc01
 * @description:
 * @author: 闫克起2
 * @create: 2023-09-18 12:00
 **/
public class OssUtils {
    private static String endpoint = "https://oss-cn-guangzhou.aliyuncs.com";
    private static String accessKeyId = "LTAI5tA7D9AWTV8AYrGRgVdv";
    private static String accessKeySecret = "OcjKQsN2kI2EmovnlpkpKCsZq1zvyB";
    private static String bucketName = "aaa01-n";

    /**
     * oss文件上传
     *
     * @param
     * @return 返回上传后的oss路径
     */
    /*public static String ossUpload(MultipartFile file) {

        // 在bucket---根据自己的endpoint来查找。
        *//*String endpoint = "oss-cn-guangzhou.aliyuncs.com";
        // accessKeyId:id编号  accessKeySecret密钥。
        String accessKeyId = "LTAI5tA7D9AWTV8AYrGRgVdv";
        String accessKeySecret = "OcjKQsN2kI2EmovnlpkpKCsZq1zvyB";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "aaa01-n";*//*

//        // 在bucket---根据自己的endpoint来查找。
//        String endpoint = "XXX";
//        // accessKeyId:id编号  accessKeySecret密钥。
//        String accessKeyId = "XXX";
//        String accessKeySecret = "XXX";
//        // 填写Bucket名称，例如examplebucket。
//        String bucketName = "gz01-888";

        // 填写Object完整路径，例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
        // 上传后的名称
        String objectName = "images/" + UUID.randomUUID().toString().replace("-", "") + file.getOriginalFilename();
//        String objectName = UUID.randomUUID().toString().replace("-", "") + file.getOriginalFilename();
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            //表示上传的本地的内容
            ossClient.putObject(bucketName, objectName, file.getInputStream());
            //https://gz01-888.oss-cn-guangzhou.aliyuncs.com/gz01.jpg
            return endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;

            return "https://" + bucketName + "." + endpoint + "/" + objectName;
        } catch (Exception ce) {

        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }*/
    public static CheckingResultImgVo ossUpload(MultipartFile multipartFile) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //  上传的文件名称
        String name = UUID.randomUUID().toString().replace("-", "") + multipartFile.getOriginalFilename();
//        String fileName = "images/" + name;
        String fileName = name;
        try {
            ossClient.putObject(bucketName, fileName, multipartFile.getInputStream());
        } catch (OSSException | ClientException | IOException oe) {
            oe.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        //  文件访问路径
        String url=endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;

        CheckingResultImgVo result=new CheckingResultImgVo();
        result.setName(name);
        result.setUrl(url);
        return result;
    }


    /**
     * oss文件删除
     *
     * @param pictureUrl 图片地址
     * @return 返回上传后的oss路径
     */
    public static void ossRemove(String pictureUrl) {
        String[] segments = pictureUrl.split("/");
        String filename = segments[segments.length - 1];

        // 在bucket---根据自己的endpoint来查找。
      /*  String endpoint = "oss-cn-guangzhou.aliyuncs.com";
        // accessKeyId:id编号  accessKeySecret密钥。
        String accessKeyId = "LTAI5tA7D9AWTV8AYrGRgVdv";
        String accessKeySecret = "OcjKQsN2kI2EmovnlpkpKCsZq1zvyB";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "aaa01-n";*/

        // 填写文件完整路径。文件完整路径中不能包含Bucket名称。
        String objectName = filename;

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 删除文件或目录。如果要删除目录，目录必须为空。
            ossClient.deleteObject(bucketName, objectName);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

}